// pages/wdyy/wdyy.js
const db = wx.cloud.database()
var times = require('../../utils/times.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        Appointment_info: [],
        show: false,
        chosen: '',
        qxid: '',
        cgid: '',
        index: '',
        Nowtime: '',
        _createTime: '',
        Violation: '',
        seat_state: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getlist();
    },
    //获取预约信息/判断是否违规
    getlist() {
        const date = new Date();
        let Year = date.getFullYear();
        let Month = date.getMonth() + 1;
        let Day = date.getDate();
        var Hours = date.getHours();
        var Minutes = date.getMinutes() - 30;
        this.setData({
            Nowtime: Year + '-' + '0' + Month + '-' + Day + ' ' + Hours + ':' + Minutes,
        });
        let phone = wx.getStorageSync('user')
        db.collection("userInfo").where({ phone: phone._id }).orderBy('_createTime', 'desc').get().then(res => {
            for (var i = 0; i < res.data.length; i++) {
                res.data[i]["_createTime"] = times.toDate(res.data[i]["_createTime"])
                res.data[i]["check_time"] = times.toDate(res.data[i]["check_time"])
                res.data[i]["_updateTime"] = times.toDate(res.data[i]["_updateTime"])
                res.data[i]["qxTime"] = times.toDate(res.data[i]["qxTime"])
            }
            this.setData({
                Appointment_info: res.data,
            })
            for (var j = 0; j < res.data.length; j++) {
                this.setData({ _createTime: res.data[j]._createTime, seat_state: res.data[j].seat_state })
                if (this.data.seat_state == 0 && !this.data._createTime == ' ' && this.data.Nowtime > this.data._createTime) {
                    console.log('超时了')
                    this.setData({ Violation: res.data[j] })
                    let cg_id = this.data.Violation.cg_id
                    let index = this.data.Violation.seat_id
                    db.collection('userInfo').doc(this.data.Violation._id).update({
                        data: {
                            seat_state: 4,
                            _updateTime: Date.parse(new Date()),
                        },
                    })
                    db.collection('time').doc(cg_id).update({
                        data: {
                            ['seat_list.' + index + '.seat_state']: 0,
                            _updateTime: Date.parse(new Date()),
                        },
                        success(res) {
                            let phone = wx.getStorageSync('user')
                            db.collection("user").doc(phone._id).update({
                                data: {
                                    user_seat: 0
                                },
                            })
                        },
                    })
                }
            }
        })
    },
    //跳转到签到
    SignIn(e) {
        console.log(e)
        wx.navigateTo({
            url: '../SignIn/SignIn?SignIn_id=' + e.currentTarget.id,
        })
    },
    //取消预约
    showPopup(e) {
        console.log(e.currentTarget.dataset)
        this.setData({
            show: true,
            qxid: e.currentTarget.dataset.value1,
            index: e.currentTarget.dataset.value2,
            cgid: e.currentTarget.dataset.value3
        });
    },
    onClose() {
        this.setData({ show: false });
    },
    //取消原因
    formSubmit(e) {
        console.log('form发生了submit事件，携带数据为：', e.detail.value)
        this.setData({
            chosen: e.detail.value.radio,
            show: false
        })
        db.collection('cancel_seat').add({
            data: {
                order_info: this.data.qxid,
                cancel_yy: this.data.chosen,
                user_xx: this.data.user,
                _createTime: Date.parse(new Date())
            },
            success: (res) => {
                db.collection('userInfo').doc(this.data.qxid).update({
                    data: {
                        cancel: 1,
                        seat_state: 3,
                        qxTime: Date.parse(new Date()),
                    },
                    success: (res => {
                        db.collection("time").doc(this.data.cgid).update({
                            data: {
                                ['seat_list.' + this.data.index + '.seat_state']: 0,
                                _updateTime: Date.parse(new Date()),
                            },
                            success: res => {
                                let phone = wx.getStorageSync('user')
                                console.log(phone, 1111)
                                db.collection("user").doc(phone._id).update({
                                    data: {
                                        user_seat: 0
                                    },
                                    success: (res => {
                                        wx.showLoading({
                                            duration: 4000
                                        })
                                        this.getlist()
                                        wx.hideLoading({
                                            success: (res) => {
                                                wx.showToast({
                                                    title: '取消成功',
                                                    icon: 'success',
                                                    duration: 2000
                                                })
                                            },
                                        })
                                    }),
                                })
                            },
                        })
                    })
                })
            }
        })
    },
    //签退
    SignOut(e) {
        this.setData({
            qxid: e.currentTarget.dataset.value1,
            index: e.currentTarget.dataset.value2,
            cgid: e.currentTarget.dataset.value3,
        })
        db.collection('userInfo').doc(this.data.qxid).update({
            data: {
                seat_state: 2,
                _updateTime: Date.parse(new Date()),
            },
            success: (res => {
                db.collection("time").doc(this.data.cgid).update({
                    data: {
                        ['seat_list.' + this.data.index + '.seat_state']: 0,
                        _updateTime: Date.parse(new Date()),
                    },
                    success: res => {
                        let phone = wx.getStorageSync('user')
                        console.log(phone, 1111)
                        db.collection("user").doc(phone._id).update({
                            data: {
                                user_seat: 0
                            },
                            success: (res => {
                                wx.showLoading({
                                    duration: 4000
                                })
                                this.getlist()
                                wx.hideLoading({
                                    success: (res) => {
                                        wx.showToast({
                                            title: '签退成功',
                                            icon: 'success',
                                            duration: 2000
                                        })
                                    },
                                })
                            }),
                        })
                    },
                })
            })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})